package com.hula.onion.book.server.spider.platforms.mindaitianxia;

import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.utils.RegexParserUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/17 19:49
 *
 * @author shan
 * @version 1.0
 */
@Component
public class MinDaiTianXiaSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form, String publicKey) throws Exception {

        Map<String, Object> map = new HashMap<>(4);

        map.put("userName", form.getUserName());
        map.put("password", form.getPassword());
        map.put("imgCode", form.getTicket());
        return map;

    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public AccountInfo parseAccountBalance(String html) {

        AccountInfo accountInfo = new AccountInfo();

        String beginRegex = "dt>资产总金额</dt>\n" +
                "\t\t\t\t\t\t<dd><span class=\"tx_sz18\">";
        String endRegex = "</span>元</dd>";
        String result = new RegexParserUtils(beginRegex, endRegex, html, RegexParserUtils.TEXTTEGEX).getText();

        accountInfo.setAmount(new BigDecimal(result));

        return accountInfo;
    }

    /**
     * 解析错误信息
     *
     * @param html
     * @return
     */
    public String parsErrorMessage(String html) {
        String beginRegex = "<p id=\"userPwMsg\">";
        String endRegex = "</p>";
        String result = new RegexParserUtils(beginRegex, endRegex, html, RegexParserUtils.TEXTTEGEX).getText();
        if (null == result) {
            beginRegex = "<div class=\"error_r\">\n" +
                    "\t\t\t\t<h3>";
            endRegex = "</h3>";
            result = new RegexParserUtils(beginRegex, endRegex, html, RegexParserUtils.TEXTTEGEX).getText();
        }

        return result;
    }

}
